import ccclass = cc._decorator.ccclass;
import property = cc._decorator.property;
import {model} from "../web/model/Model";
import {ResType} from "../config/config";
import {utils} from "../utils/utils";
import SwimShopItem from "./swimShopItem";
import {GlobalObject} from "../game/GameVar";
import Confirm from "./confirm";
import {StatEvent, Statistic} from "../../statistic/statistic";
import Menu from "../menu/menu";
import {apiUtils} from "../web/api/ApiUtils";
import {WordsUtils} from "../utils/wordsUtils";
import {wxUtils} from "../wx/wxUtils";

@ccclass
export default class ShowBoard extends cc.Component {
  @property(cc.Node)
  private btnBuy: cc.Node = null;
  @property(sp.Skeleton)
  swim: sp.Skeleton = null;
  @property(sp.Skeleton)
  headUp: sp.Skeleton = null;
  @property(sp.Skeleton)
  headDown: sp.Skeleton = null;
  @property(cc.Label)
  labelName: cc.Label = null;
  @property(cc.Label)
  labelDes: cc.Label = null;

  @property(cc.Node)
  btnGet: cc.Node = null;
  @property(cc.Node)
  btnMerge: cc.Node = null;
  @property(cc.Node)
  btnUsed: cc.Node = null;
  @property(cc.Node)
  btnUse: cc.Node = null;
  @property(cc.Node)
  bottomNode: cc.Node = null;
  @property(cc.Label)
  bottomLabel: cc.Label = null;
  @property(cc.Sprite)
  bottomSprite: cc.Sprite = null;
  @property(cc.Node)
  btnRmbBuy: cc.Node = null;

  @property({type: cc.Texture2D})
  piece: cc.Texture2D = null;
  @property({type: cc.Texture2D})
  diamond: cc.Texture2D = null;
  @property({type: cc.Texture2D})
  gold: cc.Texture2D = null;

  onLoad() {
    this.loadRole(model.user.activeRole % 6000)
    this.loadSwim(model.user.activeSwim % 5000);
    this.btnUsed.getComponent(cc.Button).interactable = false;
  }

  private _data: any = null;

  setCurrentSelect(data: any) {
    this._data = data;
    switch (data.type) {
      case ResType.skin:
        this.loadSwim(data.id % 5000);
        this.state = utils.getSwimState(data)
        break;
      case ResType.head:
        this.loadRole(data.id % 6000);
        this.updateState(utils.getHeadState(data));
        this.bottomLabel.string = data.cost.toString();
        break;
    }
    this.labelName.string = data.name;
    this.labelDes.string = data.des;
  }

  private loadRole(roleId: number) {
    if (isNaN(roleId) || roleId == null) {
      roleId = 1;
    }
    cc.loader.loadRes(`spine/human/${roleId}/down/man`, sp.SkeletonData, (error: Error, resource: any) => {
      this.headDown.skeletonData = resource;
      this.headDown.setAnimation(0, 'stand', true)
    });
    cc.loader.loadRes(`spine/human/${roleId}/up/man`, sp.SkeletonData, (error: Error, resource: any) => {
      this.headUp.skeletonData = resource;
      this.headUp.setAnimation(0, 'stand', true)
    });
  }

  private loadSwim(swimId: number) {
    if (isNaN(swimId) || swimId == null) {
      swimId = 1;
    }
    cc.loader.loadRes(`spine/swim-ring/${swimId}/e`, sp.SkeletonData, (error: Error, resource: any) => {
      this.swim.skeletonData = resource;
      this.swim.setAnimation(0, '1_', true)
    })
  }


  private _state = 0;//1 已拥有, 2 已使用, 3 可以合成, 4 没有

  get state(): number {
    return this._state;
  }

  set state(value: number) {
    if (value === this._state) {
      return;
    }
    this.btnRmbBuy.active = false;
    this.bottomNode.active = false;
    this.bottomSprite.spriteFrame = new cc.SpriteFrame(this.piece);
    this.btnBuy.active = false;
    switch (value) {
      //已经拥有, 未使用
      case 1:
        this.btnMerge.active = false;
        this.btnUsed.active = false;
        this.btnUse.active = true;
        this.btnGet.active = false;
        break;
      //已经使用
      case 2:
        this.btnMerge.active = false;
        this.btnUsed.active = true;
        this.btnUse.active = false;
        this.btnGet.active = false;
        break;
      //可以合成
      case 3:
        this.btnMerge.active = true;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = false;
        this.bottomNode.active = true;
        this.bottomLabel.string = `${model.user.getItemNum(this._data.fragmentId)}/${this._data.mergeCount}`
        break;
      //没有
      case 4:
        this.btnMerge.active = false;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = true;
        this.bottomLabel.string = `${model.user.getItemNum(this._data.fragmentId)}/${this._data.mergeCount}`
        break;
      //没有，不可合成
      case 5:
        this.btnMerge.active = false;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = true;
        break;
      //可合成，万能碎片
      case 6:
        this.btnMerge.active = true;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = false;
        break;
      case 7:
        this.btnMerge.active = false;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = false;
        this.btnRmbBuy.active = true;
        break;
    }
    this._state = value;
  }

  onMergeTouch() {
    if (this._state === 6) {
      this.node.emit('powerful-merge', this._data)
      return;
    }

    function showMerge(sc: ShowBoard) {
      GlobalObject.confirm.getComponent(Confirm).showMerge("当前皮肤碎片充足。是否立即合成", () => {
        sc.node.emit('swim-merge', sc._data.id)
      });
    }

    showMerge(this)
  }

  onGetTouch() {
    const way = this._data.way;
    Statistic.emit(StatEvent.BTN_SWIM_GET, {id: this._data.id})
    cc.director.loadScene('menu', () => {
      const menu = cc.director.getScene().getChildByName("menu").getComponent(Menu);
      menu.openBoard(way)
    })
  }

  onUseTouch() {
    model.commonResult.once('0', () => {
      if (this._data.type === 5) {
        model.user.activeSwim = this._data.id;
        this.state = 2;
      } else {
        model.user.activeRole = this._data.id;
        this.updateState(ShopState.USED);
      }
      this.node.emit('item-used')
    })
    if (this._data.type === 5) {
      apiUtils.useItem(this._data.id, 2)
    } else {
      apiUtils.useItem(this._data.id, 1)
    }
  }

  onBuyTouch() {
    if (model.user.getItemNum(this._data.currencytype) < this._data.cost) {
      return WordsUtils.warn('所需材料不足')
    }
    GlobalObject.confirm.getComponent(Confirm).showBuy(this._data.id, () => {
      apiUtils.buyItem(this._data.id);
      model.commonResult.once('0', () => {
        this.updateState(ShopState.NOT_USED);
      })
    })
  }

  async onRmbBuyTouch() {
    const prepayData = await apiUtils.prepay(this._data.id);
    wxUtils.pay(prepayData, {
      success: () => {
        this.state = 1;
      },
      fail: () => {
        console.log('支付失败')
        WordsUtils.error('支付失败');
      }
    })
  }

  private updateState(state: ShopState) {
    this.btnMerge.active = false;
    this.bottomNode.active = false;
    switch (state) {
      case ShopState.NOT_BUY:
        this.btnBuy.active = true;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = false;
        this.bottomNode.active = true;
        this.loadIcon();
        break;
      case ShopState.NOT_USED:
        this.btnBuy.active = false;
        this.btnUsed.active = false;
        this.btnGet.active = false;
        this.btnUse.active = true;
        break;
      case ShopState.USED:
        this.btnBuy.active = false;
        this.btnUsed.active = true;
        this.btnUse.active = false;
        this.btnGet.active = false;
        break;
      case ShopState.GET:
        this.btnBuy.active = false;
        this.btnUsed.active = false;
        this.btnUse.active = false;
        this.btnGet.active = true;
        break;
    }
  }

  private loadIcon() {
    if (this._data.currencytype) {
      this.bottomSprite.spriteFrame = new cc.SpriteFrame(this.diamond);
    } else {
      this.bottomSprite.spriteFrame = new cc.SpriteFrame(this.piece);
    }
  }
}

export enum ShopState {
  USED = 2,
  NOT_USED = 1,
  NOT_BUY = 3,
  GET = 4
}